<?php


namespace App\Http\Controllers\Mp;


use App\Http\Controllers\Controller;
use App\Model\Business;
use App\Model\Device;
use App\Model\Ttlock;
use App\User;
use App\Utils\Result;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class AdminController extends Controller
{
    public function create(Request $request){
        $parent = $this->getUser();
        $mobile = $request->input('mobile');
        $password = $request->input('password');
        $nickname = $request->input('nickname');
        if($mobile == $parent->mobile){
            return Result::error('1001','不可以设置自身为管理员');
        }
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        $user = User::where('mobile',$mobile)->first();
        $classId = $request->input('classId');
        if(!$classId){
            return Result::error(1001,'请选择楼栋信息');
        }
        if($user){
            $str='请登录帐号'.$mobile.':';
            $checkDel = false;
            if($user->amount > 0 ){
                $str.='1.清空帐户余额';
                $checkDel = true;
            }
            $checkTT = Ttlock::where('user_id',$user->id)->where('type',1)->count();
            if($checkTT>0){
                $str.='2.删除门锁';
                $checkDel = true;
            }
            $business = Business::where('user_id',$user->id)->first();
            if($business){
                $checkDevice = Device::query()->where('business_id',$business->id)->count();
                if($checkDevice>0){
                    $checkDel = true;
                    $str.='3.删除水表电表';
                }
            }
            $str.='(删除以上数据后重新设置)';
            if($checkDel){
                return Result::error(1001,$str);
            }
            $user->delete();
        }
        $user = new User();
        $user->mobile = $mobile;
        $user->password = $hashed_password;
        $user->parent_id = $parent->id;
        $user->class_id = $classId;
        $user->nickname = $nickname;
        $user->type = 'admin';
        if($user->save()){
            return Result::success();
        }
        return Result::error();
    }

    public function edit(Request $request){
        $id = $request->input('id');
        $user = User::find($id);
        if(!$user && $user->type != 'admin'){
            return Result::error(404,'用户不存在');
        }
        $classId = $request->input('classId');
        if(!$classId){
            return Result::error(1001,'请选择楼栋信息');
        }
        $user->class_id = $classId;
        $user->save();
        return Result::success();
    }
    public function list(Request $request){
        $user =  $this->getUser();
        $list = User::query()->where('parent_id',$user->id)
            ->where('type','admin')->get();
        return Result::success($list);
    }

    public function del(Request $request){
        $user =  $this->getUser();
        $id = $request->input('id');
        if(!$id){
            return Result::error(1001,'缺少参数id');
        }
        User::query()->where('id',$id)
            ->where('parent_id',$user->id)
            ->delete();
        return Result::success();
    }
}
